{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DesignerHint;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TfrmDesignerHint = class(TForm)
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormDeactivate(Sender: TObject);
  private
    { Private declarations }
    procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
  protected
    { Protected declarations }
    procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public declarations }
    class function ShowHint(HintInfo: THintInfo): Boolean;
  end;

implementation

{$R *.dfm}

uses ApplicationManager, UIObjects;

var
  FormInstance: TfrmDesignerHint;

class function TfrmDesignerHint.ShowHint(HintInfo: THintInfo): Boolean;
var
  UIObject: TAHMUIObject;
begin
  // Set default result - hint not shown
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmDesignerHint.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  if HintInfo.HintData <> nil then
  begin
    UIObject := TAHMUIObject(HintInfo.HintData);

    // Position and display hint form
    FormInstance.Left := HintInfo.HintPos.X;
    FormInstance.Top := HintInfo.HintPos.Y;
    FormInstance.Width := Screen.Width div 4;
    FormInstance.Height := Screen.Height div 4;
    FormInstance.Show;

    // Render preview of UIObject into form
    ApplicationManagerInstance.UIManager.RenderPreview(UIObject, FormInstance.Handle);

    // Suppress standard hint display
    Result := True;
  end
  else
  begin
    // No UIObject to render, so hide form
    FormInstance.Close;
  end;
end;

//-----------------------------------------------------------------------------
//----------------------------- Form Events -----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerHint.WMNCHitTest(var Msg: TWMNCHitTest);
begin
  // Swallow this message to prevent form being resized
  Msg.Result := htClient;
end;

procedure TfrmDesignerHint.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);

  // Hide title bar but show borders
  Params.Style := WS_POPUP or WS_THICKFRAME;
end;

procedure TfrmDesignerHint.FormDeactivate(Sender: TObject);
begin
  Close;
end;

procedure TfrmDesignerHint.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Close;
end;

end.
